/*
 * createAccountWindow.java
 *
 * Created on 11. Dezember 2003, 19:10
 */

/**
 *
 * @author  ChrM (chrmac@gmx.de)
 */

package com.jtalker.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;

public class CreateChatroomWindow extends JDialog implements
		JTalkerClientCommandInterface {
	JTalkerClientFront parent = null;

	/** Creates new form createAccountWindow */
	public CreateChatroomWindow(JTalkerClientFront parent, boolean modal) {
		super(parent, modal);
		this.parent = parent;
		this.initComponents();
		this.getRootPane().setDefaultButton(this.jButtonCreate);
		this.setLocationRelativeTo(parent);
		this.setResizable(false);
		this.setTitle("Create a Chatroom");
		this.parent.theClient.registerForCommandResponse(this);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	private void initComponents() {// GEN-BEGIN:initComponents
		jPanelImage = new JPanel();
		jLabelImage = new JLabel();
		jPanelCreateChatRoom = new JPanel();
		jLabelCreateChatRoom = new JLabel();
		jLabelRoomName = new JLabel();
		jLabelUsers = new JLabel();
		jTextFieldRoomName = new JTextField();
		jTextFieldUsers = new JTextField();
		jButtonCreate = new JButton();
		jLabelStatus = new JLabel();
		ClassLoader cl = ResourceAnchor.class.getClassLoader();

		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				closeDialog(evt);
			}
		});

		jLabelImage.setIcon(new ImageIcon(cl
				.getResource("com/jtalker/rs/icons/new_chatroom.jpg")));
		jLabelImage.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
		jPanelImage.add(jLabelImage);

		this.getContentPane().add(jPanelImage, BorderLayout.WEST);

		jPanelCreateChatRoom.setLayout(null);

		jPanelCreateChatRoom.setPreferredSize(new Dimension(300, 195));
		jLabelCreateChatRoom.setFont(new Font("Dialog", 1, 18));
		jLabelCreateChatRoom.setHorizontalAlignment(SwingConstants.CENTER);
		jLabelCreateChatRoom.setText("Create Chatroom");
		jLabelCreateChatRoom.setBounds(40, 10, 220, 25);
		jPanelCreateChatRoom.add(jLabelCreateChatRoom);

		jLabelRoomName.setHorizontalAlignment(SwingConstants.RIGHT);
		jLabelRoomName.setText("Room Name:");
		jLabelRoomName.setBounds(10, 60, 110, 18);
		jPanelCreateChatRoom.add(jLabelRoomName);

		jLabelUsers.setHorizontalAlignment(SwingConstants.RIGHT);
		jLabelUsers.setText("Users:");
		jLabelUsers.setBounds(10, 90, 110, 18);
		jPanelCreateChatRoom.add(jLabelUsers);

		jTextFieldRoomName.setToolTipText("Name for the new chatroom");
		jTextFieldRoomName.setText(this.parent.theClient.userName + "-room");
		jPanelCreateChatRoom.add(jTextFieldRoomName);
		jTextFieldRoomName.setBounds(130, 60, 150, 22);

		jTextFieldUsers.setToolTipText("Allowed users, separated by ','");
		jTextFieldUsers.setText(this.parent.theClient.userName + ",");
		jPanelCreateChatRoom.add(jTextFieldUsers);
		jTextFieldUsers.setBounds(130, 90, 150, 22);

		jButtonCreate.setText("Create");
		jButtonCreate.setToolTipText("Creates a chatroom");
		jButtonCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jPanelCreateChatRoom.add(jButtonCreate);
		jButtonCreate.setBounds(190, 130, 88, 28);

		jLabelStatus.setHorizontalAlignment(SwingConstants.CENTER);
		jLabelStatus.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
		jPanelCreateChatRoom.add(jLabelStatus);
		jLabelStatus.setBounds(10, 170, 270, 20);

		this.getContentPane().add(jPanelCreateChatRoom, BorderLayout.CENTER);

		this.pack();
	}// GEN-END:initComponents

	private void jButton1ActionPerformed(ActionEvent evt) {// GEN-FIRST:event_jButton1ActionPerformed
		// Create Account
		this.jLabelStatus.setText("Checking inputs...");
		String roomName = this.jTextFieldRoomName.getText();
		String roomUsers = this.jTextFieldUsers.getText();

		if (roomName.length() < 5) {
			this.jLabelStatus.setText("Name to short!");
			return;
		}

		if (!CreateChatroomWindow.isAllowedUserName(roomName)) {
			this.jLabelStatus.setText("Forbidden characters in name!");
			return;
		}

		// Call chat-room creation procedure
		if (this.parent.isConnected) {
			this.parent.theClient.sendMessage("std::createChatRoom(\""
					+ roomName + "\", \"" + roomUsers + "\")", null,
					"command", null, "usercommand11");
		}

		// this.closeDialog(null);
	}// GEN-LAST:event_jButton1ActionPerformed

	/** Closes the dialog */
	private void closeDialog(WindowEvent evt) {// GEN-FIRST:event_closeDialog
		setVisible(false);
		dispose();
	}// GEN-LAST:event_closeDialog

	/**
	 * Static method for checking for allowed user names
	 * 
	 * @param userName
	 *            Input String for checking
	 * @return True, if the input is an allowed user name
	 */
	public static boolean isAllowedUserName(String userName) {
		if (userName.length() > 20){
			return false;
		}
		return userName.matches("[A-Za-z0-9-]+");
	}

	/**
	 * A command answer will be send to us from the server using this method
	 * 
	 * @return true, if the _id belongs to our command
	 */
	public boolean setCommandAnswer(String id, String from, String content) {
		if (id.equals("usercommand11")) {
			// TODO check the answer (error/info/...)
			this.jLabelStatus.setText(content);
		}

		return true;
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	/*
	 * public static void main(String args[]) { new CreateChatroomWindow(new
	 * JFrame(), true).show(); }
	 */

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private JButton jButtonCreate;
	private JLabel jLabelImage;
	private JLabel jLabelCreateChatRoom;
	private JLabel jLabelRoomName;
	private JLabel jLabelUsers;
	private JLabel jLabelStatus;
	private JPanel jPanelImage;
	private JPanel jPanelCreateChatRoom;
	private JTextField jTextFieldUsers;
	private JTextField jTextFieldRoomName;
	// End of variables declaration//GEN-END:variables

}
